﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using PetaPoco;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [AppService]
    public class ArticleStatementService
    {
        private readonly ArticleStatementRepository _articleStatement;

        public ArticleStatementService(ArticleStatementRepository  articleStatement)
        {
            _articleStatement = articleStatement;
        }

        /// <summary>
        /// 后台编辑一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EditArticleStatement> GetEditAsync(int? id)
        {
            return await _articleStatement.GetEditAsync(id);
        }

        /// <summary>
        /// 取一条最新的
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ArticleStatement> GetOneByCidAsync(int cid)
        {
            return await _articleStatement.GetOneByCidAsync(cid);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="articleQuery"></param>
        /// <returns></returns>
        public async Task<Page<ViewArticleStatementAndCate>> GetListByCidAdminAsync(ArticleStatementQuery articleQuery)
        {
            articleQuery.Orderby = "Id desc";
            return await _articleStatement.GetListAsync(articleQuery);
        }

        /// <summary>
        /// 写一条新的内容
        /// </summary>
        /// <returns></returns>
        public virtual async Task<ArticleStatement> InsertArticleAndContentAsync(ArticleStatement article)
        {
            return await _articleStatement.InsertArticleAndContentAsync(article);
        }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <returns></returns>
        public virtual async Task<int> UpdateArticleAndContentAsync(EditArticleStatement article)
        {
            return await _articleStatement.UpdateArticleAndContentAsync(article);
        }


        /// <summary>
        /// 删除一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<int> DelArticleAndContentAsync(int id)
        {
            if (await _articleStatement.DeleteByPriKeyAsync(id) > 0)
            {
                await _articleStatement.DeleteByPriKeyAsync(id);
                return 1;
            }
            return 0;
        }
    }
}
